import request from '@/utils/request'

// ==================== 文件管理 ====================

// 分页查询文件
export function getFilePage(params) {
  return request({
    url: '/file/pageFile',
    method: 'get',
    params
  })
}

// 获取文件详情
export function getFileDetail(id) {
  return request({
    url: `/file/getFileById/${id}`,
    method: 'get'
  })
}

// 上传文件
export function uploadFile(data) {
  return request({
    url: '/file/upload',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 批量上传文件
export function batchUploadFiles(data) {
  return request({
    url: '/file/batchUpload',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 更新文件信息
export function updateFile(data) {
  return request({
    url: '/file/updateFile',
    method: 'post',
    data
  })
}

// 删除文件
export function deleteFile(id) {
  return request({
    url: `/file/deleteFile/${id}`,
    method: 'delete'
  })
}

// 批量删除文件
export function deleteBatchFile(ids) {
  return request({
    url: '/file/deleteBatch',
    method: 'post',
    data: ids
  })
}

// 移动文件
export function moveFile(fileId, targetFolderId) {
  return request({
    url: `/file/move/${fileId}`,
    method: 'post',
    params: { targetFolderId }
  })
}

// 复制文件
export function copyFile(fileId, targetFolderId, newName) {
  return request({
    url: `/file/copy/${fileId}`,
    method: 'post',
    params: { targetFolderId, newName }
  })
}

// 重命名文件
export function renameFile(fileId, newName) {
  return request({
    url: `/file/rename/${fileId}`,
    method: 'post',
    params: { newName }
  })
}

// 下载文件
export function downloadFile(id) {
  return request({
    url: `/file/download/${id}`,
    method: 'get',
    responseType: 'blob'
  })
}

// 批量下载文件
export function batchDownloadFiles(ids) {
  return request({
    url: '/file/batchDownload',
    method: 'post',
    data: ids,
    responseType: 'blob'
  })
}

// 分享文件
export function shareFile(id, shareType, expireTime, password) {
  return request({
    url: `/file/share/${id}`,
    method: 'post',
    params: { shareType, expireTime, password }
  })
}

// 取消分享
export function unshareFile(id) {
  return request({
    url: `/file/unshare/${id}`,
    method: 'post'
  })
}

// 搜索文件
export function searchFiles(keyword, params) {
  return request({
    url: '/file/search',
    method: 'get',
    params: { keyword, ...params }
  })
}

// 获取文件预览URL
export function getFilePreviewUrl(id) {
  return request({
    url: `/file/preview/${id}`,
    method: 'get'
  })
}

// 获取文件缩略图
export function getFileThumbnail(id) {
  return request({
    url: `/file/thumbnail/${id}`,
    method: 'get',
    responseType: 'blob'
  })
}

// ==================== 文件夹管理 ====================

// 获取文件夹树
export function getFolderTree(parentId) {
  return request({
    url: '/folder/tree',
    method: 'get',
    params: { parentId }
  })
}

// 获取文件夹详情
export function getFolderDetail(id) {
  return request({
    url: `/folder/getFolderById/${id}`,
    method: 'get'
  })
}

// 创建文件夹
export function createFolder(data) {
  return request({
    url: '/folder/createFolder',
    method: 'post',
    data
  })
}

// 更新文件夹
export function updateFolder(data) {
  return request({
    url: '/folder/updateFolder',
    method: 'post',
    data
  })
}

// 删除文件夹
export function deleteFolder(id) {
  return request({
    url: `/folder/deleteFolder/${id}`,
    method: 'delete'
  })
}

// 移动文件夹
export function moveFolder(folderId, targetFolderId) {
  return request({
    url: `/folder/move/${folderId}`,
    method: 'post',
    params: { targetFolderId }
  })
}

// 复制文件夹
export function copyFolder(folderId, targetFolderId, newName) {
  return request({
    url: `/folder/copy/${folderId}`,
    method: 'post',
    params: { targetFolderId, newName }
  })
}

// 重命名文件夹
export function renameFolder(folderId, newName) {
  return request({
    url: `/folder/rename/${folderId}`,
    method: 'post',
    params: { newName }
  })
}

// 获取文件夹内容
export function getFolderContents(folderId, params) {
  return request({
    url: `/folder/contents/${folderId}`,
    method: 'get',
    params
  })
}

// ==================== 存储统计 ====================

// 获取存储统计
export function getStorageStatistics() {
  return request({
    url: '/storage/statistics',
    method: 'get'
  })
}

// 获取用户存储统计
export function getUserStorageStatistics(userId) {
  return request({
    url: `/storage/user/${userId}/statistics`,
    method: 'get'
  })
}

// 获取文件类型统计
export function getFileTypeStatistics() {
  return request({
    url: '/storage/file-type-statistics',
    method: 'get'
  })
}

// 获取存储使用趋势
export function getStorageUsageTrend(params) {
  return request({
    url: '/storage/usage-trend',
    method: 'get',
    params
  })
}

// ==================== 回收站管理 ====================

// 获取回收站文件
export function getRecycleBinFiles(params) {
  return request({
    url: '/recycle-bin/files',
    method: 'get',
    params
  })
}

// 恢复文件
export function restoreFile(id) {
  return request({
    url: `/recycle-bin/restore/${id}`,
    method: 'post'
  })
}

// 批量恢复文件
export function batchRestoreFiles(ids) {
  return request({
    url: '/recycle-bin/batchRestore',
    method: 'post',
    data: ids
  })
}

// 彻底删除文件
export function permanentDeleteFile(id) {
  return request({
    url: `/recycle-bin/permanentDelete/${id}`,
    method: 'delete'
  })
}

// 批量彻底删除文件
export function batchPermanentDeleteFiles(ids) {
  return request({
    url: '/recycle-bin/batchPermanentDelete',
    method: 'delete',
    data: ids
  })
}

// 清空回收站
export function emptyRecycleBin() {
  return request({
    url: '/recycle-bin/empty',
    method: 'delete'
  })
}

// ==================== 文件分享管理 ====================

// 获取分享列表
export function getShareList(params) {
  return request({
    url: '/file-share/list',
    method: 'get',
    params
  })
}

// 获取分享详情
export function getShareDetail(shareId) {
  return request({
    url: `/file-share/detail/${shareId}`,
    method: 'get'
  })
}

// 取消分享
export function cancelShare(shareId) {
  return request({
    url: `/file-share/cancel/${shareId}`,
    method: 'post'
  })
}

// 更新分享设置
export function updateShareSettings(shareId, data) {
  return request({
    url: `/file-share/update/${shareId}`,
    method: 'post',
    data
  })
}
